import 'package:flutter/material.dart';
import 'package:star_author/utils/common_util/log_util.dart';
import 'package:star_author/widgets/custom_dialog/custom_alert_dialog.dart';
import 'package:permission_handler/permission_handler.dart';

class CommonPermissionHandler {
  static String photosRequestPermissionTip = "回流App需要您的允许才能访问您的相册";
  static String cameraRequestPermissionTip = "回流App需要您的允许才能访问您的相机";
  static String microphoneRequestPermissionTip = "回流App需要您的允许才能访问您的麦克风";
  static String storageRequestPermissionTip = "回流App需要您的允许才能访问您的文件操作";
  static String pushNotificationRequestPermissionTip = "回流App需要您的允许才能推送消息";

  //iOS相册权限
  static Future<void> iosPhotosRequestPermission(
      BuildContext context, VoidCallback onPermissionGranted,
      {PermissionStatus? permissionStatus}) async {
    if (permissionStatus == null) {
      permissionStatus = await Permission.photos.status;
    }
    LogUtil.v(permissionStatus);
    _permissionTypeCallback(
      permissionStatus: permissionStatus,
      context: context,
      onPermissionGranted: onPermissionGranted,
      againRequestPermission: () async {
        PermissionStatus status = await Permission.photos.request();
        iosPhotosRequestPermission(
          context,
          onPermissionGranted,
          permissionStatus: status,
        );
      },
      dialogContent: photosRequestPermissionTip,
    );
  }

  //Android 保存到相册权限
  static Future<void> storageRequestPermission(
      BuildContext context, VoidCallback onPermissionGranted,
      {PermissionStatus? permissionStatus}) async {
    if (permissionStatus == null) {
      permissionStatus = await Permission.storage.status;
    }
    LogUtil.v(permissionStatus);
    _permissionTypeCallback(
      permissionStatus: permissionStatus,
      context: context,
      onPermissionGranted: onPermissionGranted,
      againRequestPermission: () async {
        PermissionStatus status = await Permission.storage.request();
        storageRequestPermission(
          context,
          onPermissionGranted,
          permissionStatus: status,
        );
      },
      dialogContent: photosRequestPermissionTip,
    );
  }

  //相机权限
  static Future<void> cameraRequestPermission(
      BuildContext context, VoidCallback onPermissionGranted,
      {PermissionStatus? permissionStatus}) async {
    if (permissionStatus == null) {
      permissionStatus = await Permission.camera.status;
    }
    LogUtil.v(permissionStatus);
    _permissionTypeCallback(
      permissionStatus: permissionStatus,
      context: context,
      onPermissionGranted: onPermissionGranted,
      againRequestPermission: () async {
        PermissionStatus status = await Permission.camera.request();
        cameraRequestPermission(
          context,
          onPermissionGranted,
          permissionStatus: status,
        );
      },
      dialogContent: cameraRequestPermissionTip,
    );
  }

  static void _permissionTypeCallback({
    required PermissionStatus permissionStatus,
    required VoidCallback onPermissionGranted,
    required VoidCallback againRequestPermission,
    required BuildContext context,
    required String dialogContent,
  }) async {
    if (permissionStatus.isGranted) {
      onPermissionGranted();
    } else if (permissionStatus.isDenied) {
      againRequestPermission();
      // showTipDialog(context, content: dialogContent);
    } else if (permissionStatus.isPermanentlyDenied) {
      //如果用户拒绝访问请求的特性，并选择永不 再次显示此权限的请求。用户仍然可以更改 设置中的权限状态。Android
      showTipDialog(context, content: dialogContent);
    } else if (permissionStatus.isRestricted) {
      //系统权限关闭 iOS
      showTipDialog(context, content: dialogContent);
    } else if (permissionStatus.isLimited) {
      //用户已授权此应用程序进行有限访问。iOS14+
      onPermissionGranted();
    }
  }

  static void showTipDialog(BuildContext context, {required String content}) {
    ///拒接访问
    showDialog(
      context: context,
      builder: (context) {
        return CustomAlertDialog(
          title: '请开启权限',
          content: content,
          confirmCallback: () {
            openAppSettings();
          },
          isShowCancelButton: true,
          confirmBtnStr: "去开启",
        );
      },
    );
  }
}
